export default {
  data() {
    return {
      api: null,
      rowId: null,
      queryParams: {
        sortProperty: null,
        sortDirection: null
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      detail: {},
      height: document.documentElement.clientHeight - 240,
      loading: true,
      addVisible: false,
      detailVisible: false,
      editVisible: false,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  methods: {
    setApi(api) {
      this.api = api
    },
    handleAdd() {
      this.addVisible = true
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return this.api.remove(ids)
      }).then(() => {
        this.$message.success('删除成功')
        this.handleQuery()
      })
    },
    handleQuery() {
      this.page.current = 1
      this.getList()
    },
    getList() {
      this.loading = true
      this.api.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleImport() {

    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return this.api.exportData(this.queryParams)
      }).then(res => {
        this.$common.download(res.data)
      })
    },
    handleExportBatch() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return this.api.exportData(this.queryParams)
      }).then(res => {
        this.$common.download(res.data)
      })
    },
    handleTableSet() {

    },
    handleDetailClose() {
      this.detailVisible = false
      this.handleQuery()
    },
    handleSortChange(sort) {
      if (sort.order) {
        this.queryParams.sortProperty = sort.prop
        this.queryParams.sortDirection = sort.order === 'ascending' ? 'ASC' : 'DESC'
      } else {
        this.queryParams.sortProperty = null
        this.queryParams.sortDirection = null
      }
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    }
  }
}
